package com.hong.taotaomall.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.hong.taotaomall.common.BaseContext;
import com.hong.taotaomall.common.CustomException;
import com.hong.taotaomall.dto.GoodsDto;
import com.hong.taotaomall.entity.Goods;
import com.hong.taotaomall.entity.GoodsFlag;
import com.hong.taotaomall.mapper.GoodsMapper;
import com.hong.taotaomall.service.IGoodsFlagService;
import com.hong.taotaomall.service.IGoodsService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 * 商品管理 服务实现类
 * </p>
 *
 * @author hong
 * @since 2023-03-25
 */
@Service
public class GoodsServiceImpl extends ServiceImpl<GoodsMapper, Goods> implements IGoodsService {
    @Autowired
    private IGoodsFlagService goodsFlagService;


    @Override
    public void saveWithFlag(GoodsDto goodsDto) {
        try {
            this.save(goodsDto);
        } catch (Exception e) {
            System.err.println(e.getMessage());
           throw new CustomException("不能保存相同标签,请修改后重试");
        }
        Long id = goodsDto.getId();
        List<GoodsFlag> flags = goodsDto.getFlags();
        flags = flags.stream().map( item ->{
            item.setGoodsId(id);
            return item;
        }).collect(Collectors.toList());
        goodsFlagService.saveBatch(flags);
    }



    @Override
    public void updateWithFlag(GoodsDto goodsDto) {
        this.updateById(goodsDto);
        LambdaQueryWrapper<GoodsFlag> lqw = new LambdaQueryWrapper<>();
        lqw.eq(GoodsFlag::getGoodsId,goodsDto.getId());
        goodsFlagService.remove(lqw);
        List<GoodsFlag> flags = goodsDto.getFlags();
        flags = flags.stream().map( item ->{
            item.setGoodsId(goodsDto.getId());
            item.setUpdateUser(BaseContext.getCurrentId());
            return item;
        }).collect(Collectors.toList());
        try {
            goodsFlagService.saveBatch(flags);
        } catch (Exception e) {
            throw new CustomException("标签已存在,请修改后重试");
        }
    }

    @Override
    public GoodsDto getByIdWithFlag(Long id) {
        Goods goods = this.getById(id);
        GoodsDto goodsDto = new GoodsDto();
        BeanUtils.copyProperties(goods,goodsDto);
        LambdaQueryWrapper<GoodsFlag> lqw = new LambdaQueryWrapper<>();
        lqw.eq(GoodsFlag::getGoodsId,goods.getId());

        List<GoodsFlag> list = goodsFlagService.list(lqw);
        goodsDto.setFlags(list);
        return goodsDto;
    }

    @Transactional
    @Override
    public void deleteByIds(List<Long> ids) {
        LambdaQueryWrapper<Goods> lqw = new LambdaQueryWrapper<>();
        lqw.in(ids != null, Goods::getId, ids);
        List<Goods> list = this.list(lqw);
        for (Goods goods : list) {
            Integer status = goods.getStatus();
            if (status == 0){
                this.removeById(goods.getId());
            }else {
                throw new CustomException("删除商品中有正在售卖商品,无法全部删除");
            }
        }
    }
}
